package org.insist.best.codeGenerator.dao;

import cn.hutool.db.ds.simple.SimpleDataSource;
import cn.hutool.json.JSONObject;
import org.insist.best.codeGenerator.core.DataSourceUrlBuilderFactory;
import org.insist.best.codeGenerator.core.DbConfig;
import org.insist.best.codeGenerator.vo.ColumnListItem;
import org.insist.best.codeGenerator.vo.TableListItem;

import javax.sql.DataSource;
import java.sql.Connection;
import java.sql.SQLException;
import java.util.List;
import java.util.Map;

public abstract class DataBaseInfoDao {

    private DbConfig dbConfig;

    public DataBaseInfoDao (DbConfig dbConfig) {
        this.dbConfig = dbConfig;
    }

    public abstract Map<String, String> tableInfo(String tableName);

    public abstract JSONObject queryList(String queryTableName, int page, int pageSize);

    protected Connection getConnection() throws SQLException {
        DataSource dataSource = new SimpleDataSource(DataSourceUrlBuilderFactory.builderOfName(dbConfig.getDbType())
        .url(dbConfig.getHost(), dbConfig.getPort(), dbConfig.getDbName()), dbConfig.getUserName(), dbConfig.getPassword(),
                dbConfig.getDbType().getDriverClassName());
        return dataSource.getConnection();
    }

    public abstract TableListItem queryTable(String tableName);

    public abstract List<ColumnListItem> queryColumns(String tableName);
}
